package client.object;

import java.io.FileNotFoundException;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

import org.jdesktop.j3d.loaders.vrml97.VrmlLoader;

import wiiremotej.WiiRemote;

import client.behavior.WiimoteManager;

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;

public class PObject3DImpl extends TransformGroup implements PObject3D {

	private CObject3DClient controller;

	public PObject3DImpl(CObject3DClient controller) {
		super();
		this.controller = controller;
		
		String path;
		
		VrmlLoader loader = new VrmlLoader();
		path = "ressources/"+controller.getGeometry()+".wrl";
		Scene loadedScene;
		try {
			loadedScene = loader.load(path);
			BranchGroup branch = loadedScene.getSceneGroup();
			TransformGroup group = new TransformGroup();
			group.addChild(branch);
			Transform3D transform3d = new Transform3D();
			transform3d.setTranslation(controller.getPosition());
			group.setTransform(transform3d);
			addChild(group);
			this.setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE) ;
			this.setCapability (TransformGroup.ALLOW_TRANSFORM_READ) ;
			this.setCapability (TransformGroup.ENABLE_PICK_REPORTING) ;
			
			WiimoteManager wiimoteManager = WiimoteManager.getInstance();
			WiiRemote wiiRemote = wiimoteManager.getWiimote(null);
			wiimoteManager.linkWiimoteToCObject3d(wiiRemote, controller);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IncorrectFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParsingErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	@Override
	public CObject3DClient getController() {
		return controller;
	}

	@Override
	public void updateTransform() {

		
	}

}
